/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.slice;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.datamodel.ColorProcessor;
import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.core.datamodel.Scalar;
import com.cv4j.core.pixels.PrincipalColorExtractor;

import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Image;
import ohos.agp.components.element.ShapeElement;

import java.util.List;

/**
 * PrincipalColorExtractorAbilitySlice
 *
 * @since 2021-05-14
 */
public class PrincipalColorExtractorAbilitySlice extends BaseAbilitySlice {
    private Image image;
    private Image result;
    private Image result1;
    private Image result2;
    private Image result3;
    private Image result4;
    private ShapeElement element;
    private ShapeElement element1;
    private ShapeElement element2;
    private ShapeElement element3;
    private ShapeElement element4;

    /**
     * getUIContentLayoutId
     *
     * @return int
     */
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_principal_color_extractor;
    }

    /**
     * 操作类
     *
     * @param intent 意图
     */
    protected void initComponents(Intent intent) {
        initView();
        initData();
    }

    private void initView() {
        image = (Image) findComponentById(ResourceTable.Id_principal_image);
        result = (Image) findComponentById(ResourceTable.Id_result0);
        result1 = (Image) findComponentById(ResourceTable.Id_result1);
        result2 = (Image) findComponentById(ResourceTable.Id_result2);
        result3 = (Image) findComponentById(ResourceTable.Id_result3);
        result4 = (Image) findComponentById(ResourceTable.Id_result4);
    }

    private void initData() {
        image.setPixelMap(ResourceTable.Media_test_hist2);
        CV4JImage cv4jImage = new CV4JImage(image.getPixelMap());
        ImageProcessor imageProcessor = cv4jImage.getProcessor();
        PrincipalColorExtractor extractor = new PrincipalColorExtractor();
        List<Scalar> scalars = extractor.extract((ColorProcessor) imageProcessor);
        element = new ShapeElement();
        Scalar scalar0 = scalars.get(0);
        element.setRgbColor(new RgbColor(scalar0.red, scalar0.green, scalar0.blue));
        result.setBackground(element);
        element1 = new ShapeElement();
        Scalar scalar1 = scalars.get(1);
        element1.setRgbColor(new RgbColor(scalar1.red, scalar1.green, scalar1.blue));
        result1.setBackground(element1);
        element2 = new ShapeElement();
        final int posi = 2;
        final int posi1 = 3;
        final int posi2 = 4;
        Scalar scalar2 = scalars.get(posi);
        element2.setRgbColor(new RgbColor(scalar2.red, scalar2.green, scalar2.blue));
        result2.setBackground(element2);
        element3 = new ShapeElement();
        Scalar scalar3 = scalars.get(posi1);
        element3.setRgbColor(new RgbColor(scalar3.red, scalar3.green, scalar3.blue));
        result3.setBackground(element3);
        element4 = new ShapeElement();
        Scalar scalar4 = scalars.get(posi2);
        element4.setRgbColor(new RgbColor(scalar4.red, scalar4.green, scalar4.blue));
        result4.setBackground(element4);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
